#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

/*-------------------------------------------------------------------*/
#define DATA_FILE_PATH		"./data.txt"
#define MAX_COUNT		2000

#define TARGET_WANT	107
#define ZONE1_WANT		2
#define ZONE2_WANT		3
#define ZONE3_WANT		1

#define ODDS_WANT		2
#define EVENS_WART		4

/*-------------------------------------------------------------------*/
typedef struct ssq_data
{
	int r1;
	int r2;
	int r3;
	int r4;
	int r5;
	int r6;
	int b;
}ssq;

typedef struct history_data
{
	int no;
	ssq data;
}history;

int lost_r[34];
int lost_b[17];
int *all_lost;

int odd_even[MAX_COUNT];

int zone1[MAX_COUNT], zone2[MAX_COUNT], zone3[MAX_COUNT];
int red_small[MAX_COUNT], red_big[MAX_COUNT];

int data_odd[MAX_COUNT], data_even[MAX_COUNT];

int r1_remain[MAX_COUNT], r2_remain[MAX_COUNT], r3_remain[MAX_COUNT], r4_remain[MAX_COUNT], r5_remain[MAX_COUNT], r6_remain[MAX_COUNT];
/*-------------------------------------------------------------------*/

/*-------------------------------------------------------------------*/
int total_num = 0;
history total_ssq[MAX_COUNT];

FILE *data_fp = NULL;

/*-------------------------------------------------------------------*/

int scan_data(char *data_file)
{
	int i;

	data_fp = fopen(data_file, "r");

	if (data_fp == NULL)
	{
		printf("cannot open data file! %s\n", data_file);
		exit(-1);
	}

	for (i=0; i<MAX_COUNT; i++)
	{
		int r;
		r = fscanf(data_fp, "%d %02d %02d %02d %02d %02d %02d %02d\n",
			&total_ssq[i].no, &total_ssq[i].data.r1, &total_ssq[i].data.r2, &total_ssq[i].data.r3,
			&total_ssq[i].data.r4, &total_ssq[i].data.r5, &total_ssq[i].data.r6, &total_ssq[i].data.b);

		if (r<0)
			break;


		total_num ++;
	}


#if 0
	printf("total %d data!\n", total_num);
	for (i=0; i<total_num; i++)
	{
		printf("%d | %02d | %02d | %02d | %02d | %02d | %02d | %02d\n",
			total_ssq[i].no, total_ssq[i].data.r1, total_ssq[i].data.r2, total_ssq[i].data.r3,
			total_ssq[i].data.r4, total_ssq[i].data.r5, total_ssq[i].data.r6, total_ssq[i].data.b);
	}
#endif

	return 0;
}

int count_sum()
{
	int i;
	int sum;
	int count[184];
	float percent;

	int count_big = 0, count_small = 0;
	int count_odd = 0, count_even = 0;

	int max1 = 0, count1 = 0, max2 = 0, count2 = 0, max3 = 0, count3 = 0;

	int avail_lost = 0;

	memset(count, 0, sizeof(count));

	memset(lost_b, 0, sizeof(lost_b));
	memset(lost_r, 0, sizeof(lost_r));

	all_lost = malloc(sizeof(int)*total_num);

	for (i=0; i<total_num; i++)
	{
		sum = total_ssq[i].data.r1 + total_ssq[i].data.r2 + total_ssq[i].data.r3 + total_ssq[i].data.r4 + total_ssq[i].data.r5 + total_ssq[i].data.r6;

#if 0
		printf("%d | %02d | %02d | %02d | %02d | %02d | %02d | %02d  --- sum=%d\n",
			total_ssq[i].no, total_ssq[i].data.r1, total_ssq[i].data.r2, total_ssq[i].data.r3,
			total_ssq[i].data.r4, total_ssq[i].data.r5, total_ssq[i].data.r6, total_ssq[i].data.b, sum);
#endif

#if 1
		
#endif

#if 1
		if (sum == TARGET_WANT)
			printf("%d | %02d | %02d | %02d | %02d | %02d | %02d | %02d\n",
			total_ssq[i].no, total_ssq[i].data.r1, total_ssq[i].data.r2, total_ssq[i].data.r3,
			total_ssq[i].data.r4, total_ssq[i].data.r5, total_ssq[i].data.r6, total_ssq[i].data.b);
#endif 
		count[sum]++;

		if (sum > 112)
		{
			count_big++;
			if (sum%2)
					count_odd++;
				else
					count_even++;
		}
		else if (sum < 102)
			count_small++;



	}

#if 1
	for (i = total_num-1; i>=0; i--)
	{
#if 1
		r1_remain[i]=total_ssq[i].data.r1%3;
		r2_remain[i]=total_ssq[i].data.r2%3;
		r3_remain[i]=total_ssq[i].data.r3%3;
		r4_remain[i]=total_ssq[i].data.r4%3;
		r5_remain[i]=total_ssq[i].data.r5%3;
		r6_remain[i]=total_ssq[i].data.r6%3;
#endif
#if 1
		zone1[i] = 0;
		zone2[i] = 0;
		zone3[i] = 0;
		if (total_ssq[i].data.r1 <= 11)
			zone1[i]++;
		else
		{
			if (total_ssq[i].data.r1 <= 22)
				zone2[i]++;
			else
				zone3[i]++;
		}
		if (total_ssq[i].data.r2 <= 11)
			zone1[i]++;
		else
		{
			if (total_ssq[i].data.r2 <= 22)
				zone2[i]++;
			else
				zone3[i]++;
		}
		if (total_ssq[i].data.r3 <= 11)
			zone1[i]++;
		else
		{
			if (total_ssq[i].data.r3 <= 22)
				zone2[i]++;
			else
				zone3[i]++;
		}
		if (total_ssq[i].data.r4<= 11)
			zone1[i]++;
		else
		{
			if (total_ssq[i].data.r4<= 22)
				zone2[i]++;
			else
				zone3[i]++;
		}
		if (total_ssq[i].data.r5 <= 11)
			zone1[i]++;
		else
		{
			if (total_ssq[i].data.r5 <= 22)
				zone2[i]++;
			else
				zone3[i]++;
		}
		if (total_ssq[i].data.r6 <= 11)
			zone1[i]++;
		else
		{
			if (total_ssq[i].data.r6 <= 22)
				zone2[i]++;
			else
				zone3[i]++;
		}
#endif

#if 1
		red_small[i]=0;
		red_big[i]=0;
		if (total_ssq[i].data.r1 < 17)
			red_small[i]++;
		else if (total_ssq[i].data.r1 > 17)
			red_big[i]++;
		if (total_ssq[i].data.r2 < 17)
			red_small[i]++;
		else if (total_ssq[i].data.r2 > 17)
			red_big[i]++;
		if (total_ssq[i].data.r3 < 17)
			red_small[i]++;
		else if (total_ssq[i].data.r3 > 17)
			red_big[i]++;
		if (total_ssq[i].data.r4 < 17)
			red_small[i]++;
		else if (total_ssq[i].data.r4 > 17)
			red_big[i]++;
		if (total_ssq[i].data.r5 < 17)
			red_small[i]++;
		else if (total_ssq[i].data.r5 > 17)
			red_big[i]++;
		if (total_ssq[i].data.r6 < 17)
			red_small[i]++;
		else if (total_ssq[i].data.r6 > 17)
			red_big[i]++;
#endif

#if 1
		data_odd[i] = 0;
		data_even[i] = 0;

		if ((total_ssq[i].data.r1%2)==0)
			data_even[i]++;
		else
			data_odd[i]++;
		if ((total_ssq[i].data.r2%2)==0)
			data_even[i]++;
		else
			data_odd[i]++;
		if ((total_ssq[i].data.r3%2)==0)
			data_even[i]++;
		else
			data_odd[i]++;
		if ((total_ssq[i].data.r4%2)==0)
			data_even[i]++;
		else
			data_odd[i]++;
		if ((total_ssq[i].data.r5%2)==0)
			data_even[i]++;
		else
			data_odd[i]++;
		if ((total_ssq[i].data.r6%2)==0)
			data_even[i]++;
		else
			data_odd[i]++;
#endif

		int j;
		for (j = 1; j <= 33; j++)
		{
			lost_r[j]++;
		}
		lost_r[total_ssq[i].data.r1] = 0;
		lost_r[total_ssq[i].data.r2] = 0;
		lost_r[total_ssq[i].data.r3] = 0;
		lost_r[total_ssq[i].data.r4] = 0;
		lost_r[total_ssq[i].data.r5] = 0;
		lost_r[total_ssq[i].data.r6] = 0;

		for (j = 1; j < 17; j++)
		{
			lost_b[j]++;
		}
		lost_b[total_ssq[i].data.b] = 0;

		for (j = 01; j <= 33; j++)
		{
			all_lost[i] += lost_r[j];
			//printf("%d:%d; ", j, lost_r[j]);
		}
		//printf("\n");

		sum = total_ssq[i].data.r1 + total_ssq[i].data.r2 + total_ssq[i].data.r3 + total_ssq[i].data.r4 + total_ssq[i].data.r5 + total_ssq[i].data.r6;

		printf("%d | %02d | %02d | %02d | %02d | %02d | %02d || %02d ---sum=%3d, all_lost=%d, zone=(%d, %d, %d), small_big=(%d, %d), odd_even=(%d, %d), remain(%d, %d, %d, %d, %d, %d)\n",
			total_ssq[i].no, total_ssq[i].data.r1, total_ssq[i].data.r2, total_ssq[i].data.r3,
			total_ssq[i].data.r4, total_ssq[i].data.r5, total_ssq[i].data.r6, total_ssq[i].data.b, sum, all_lost[i], zone1[i], zone2[i], zone3[i],red_small[i], red_big[i],
			data_odd[i], data_even[i], r1_remain[i], r2_remain[i], r3_remain[i], r4_remain[i], r5_remain[i], r6_remain[i]);

		avail_lost += all_lost[i];
	}
	printf("avail_lost=%d\n", avail_lost/total_num);
#endif

#if 1
		printf("count_big=%d, count_small=%d, count_odd=%d, count_even=%d\n", count_big, count_small, count_odd, count_even);
#endif

	for (i = 21; i < 184; i++)
	{
		percent = (float)(count[i]*100.0)/total_num;
		printf("%d has : count=%d, percnet=%2.2f%%\n", i, count[i], percent);

		if (count[i] > count1)
		{
			count3 = count2;
			max3 = max2;
			count2 = count1;
			max2 = max1;

			count1 = count[i];
			max1 = i;
		}
		else if (count[i] > count2)
		{
			count3 = count2;
			max3 = max2;

			count2 = count[i];
			max2 = i;			
		}
		else if (count[i] > count3)
		{
			count3 = count[i];
			max3 = i;			
		}
		
	}

	printf("1: sum=%d, count=%d,  percnet=%2.2f%%\n", max1, count1, (float)(count1*100.0)/total_num);
	printf("2: sum=%d, count=%d,  percnet=%2.2f%%\n", max2, count2, (float)(count2*100.0)/total_num);
	printf("3: sum=%d, count=%d,  percnet=%2.2f%%\n", max3, count3, (float)(count3*100.0)/total_num);

	return 0;
}


ssq conbination(int start, int end, int num)
{
	int i, j, k, l, m, n, x, y, z;

	int min = start, max = end-num+1;

	int total = 0;

	int other;

	int max1 = 0, count1 = 0, max2 = 0, count2 = 0, max3 = 0, count3 = 0;

	int z1, z2, z3;
	int odd, even;

	int sum;
	int count[184];
	float percent;
	memset(count, 0, sizeof(count));

	int remain_count;

	for (i = min; i <= max; i++)
		for (j = i+1; j <= max+1; j++)
			for (k = j+1; k <= max+2; k++)
				for (l = k+1; l <=max+3; l++)
					for(m = l+1; m <= max+4; m++)
						for (n = m+1; n <= max+5; n++)
							{
								//if ((i!=1)||(j!=6)||(k!=8)||(l!=16)||(m!=29)||(n!=33))
								//	continue;
								//printf("check: %02d | %02d | %02d | %02d | %02d | %02d\n",
								//	i, j, k, l, m, n);


#if 1
								if (i > 10) 
									continue;

								if ((j==(i+1)) && (k==(j+1)))
									continue;
								if ((k==(j+1)) && (l==(k+1)))
									continue;
								if ((l==(k+1)) && (m==(l+1)))
									continue;
								if ((m==(l+1)) && (n==(m+1)))
									continue;
#endif
#if 1
								//if ((j==(i+1)) || (k==(j+1)) || (l==(k+1)) || (m==(l+1)) || (n==(m+1)))
									//continue;
#endif

								int killnum[]={03, 14, 22, 24, 33};
								int kill_flag;

								kill_flag=0;
								for (x=0; x<sizeof(killnum);x++)
								{
									if ((i == killnum[x]) || (j == killnum[x]) || (k == killnum[x]) || (l == killnum[x]) || (m == killnum[x]) || (n == killnum[x]))
									{
										kill_flag=1;
										break;
									}
								}
								if (kill_flag == 1)
									continue;

								//other = 8;
								//if ((i == other) || (j == other) || (k == other) || (l == other) || (m == other) || (n == other))
									//continue;

								//other = 18;
								//if ((i == other) || (j == other) || (k == other) || (l == other) || (m == other) || (n == other))
								//	continue;

								//other = 24;
								//if ((i == other) || (j == other) || (k == other) || (l == other) || (m == other) || (n == other))
								//	continue;

								//other = 14;
								//if ((i != other) && (j != other) && (k != other) && (l != other) && (m != other) && (n != other))
								//	continue;

								//other = 5;
								//if ((i != other) && (j != other) && (k != other) && (l != other) && (m != other) && (n != other))
								//	continue;

								int avail_lost = 0;
								avail_lost = 0;
								for (x=1; x<=33; x++)
								{
									avail_lost += lost_r[x];
									//printf("%d-%d, ", x, lost_r[x]);
								}
								//printf("\navail_lost=%d\n", avail_lost);
								avail_lost = avail_lost+33-6;
								avail_lost -= lost_r[i];
								avail_lost -= lost_r[j];
								avail_lost -= lost_r[k];
								avail_lost -= lost_r[l];
								avail_lost -= lost_r[m];
								avail_lost -= lost_r[n];

								//if ((avail_lost % 2) != 0)
								//	continue;

								if ((avail_lost <= 140) || (avail_lost >= 157))
									continue;

								//if ((i%2)==0)
								//	continue;

								//if ((i%3)!=1)
								//	continue;

								if ((l%3)==2)
									continue;

								remain_count = (i%3)+(j%3)+(k%3)+(l%3)+(m%3)+(n%3);
								if ((remain_count < 5) || (remain_count > 9))
									continue;

								if (remain_count >= 7)
									continue;

								sum = i+j+k+l+m+n;
								count[sum] ++;

								//if (sum != TARGET_WANT)
								//	continue;
								if ((sum<89) || (sum>105))
									continue;

								//if ((sum%2)==0)
								//	continue;

								z1 = z2 = z3 = 0;
								if (i <= 11)
									z1++;
								else
								{
									if (i <= 22)
										z2++;
									else
										z3++;
								}
								if (j<= 11)
									z1++;
								else
								{
									if (j <= 22)
										z2++;
									else
										z3++;
								}
								if (k <= 11)
									z1++;
								else
								{
									if (k <= 22)
										z2++;
									else
										z3++;
								}
								if (l <= 11)
									z1++;
								else
								{
									if (l <= 22)
										z2++;
									else
										z3++;
								}
								if (m <= 11)
									z1++;
								else
								{
									if (m <= 22)
										z2++;
									else
										z3++;
								}
								if (n <= 11)
									z1++;
								else
								{
									if (n <= 22)
										z2++;
									else
										z3++;
								}

								if ((z1 == 0) || (z2 == 0) || (z3 == 0))
									continue;

								//if (z1 == 2)
								//	continue;

								//if ((z1 != 2) || (z2 != 2) || (z3 != 2))
								//	continue;
								
								//if ((z1 != ZONE1_WANT) || (z2 != ZONE2_WANT) || (z3 != ZONE3_WANT))
								//	continue;

								odd = 0; even = 0;
								if ((i%2)==1)
									odd++;
								else
									even++;

								if ((j%2)==1)
									odd++;
								else
									even++;

								if ((k%2)==1)
									odd++;
								else
									even++;

								if ((l%2)==1)
									odd++;
								else
									even++;

								if ((m%2)==1)
									odd++;
								else
									even++;

								if ((n%2)==1)
									odd++;
								else
									even++;

								//printf("odd=%d, even=%d\n", odd, even);
								if ((odd < 1) || (odd > 5))
									continue;
								//if ((odd != ODDS_WANT) || (even != EVENS_WART))
									//continue;
								//if ((odd == 2) || (odd == 4))
								//	continue;

								if (check_last_same(5, 2, i, j, k, l, m, n))
									continue;

								if (check_last(0, i, j, k, l, m, n))
									continue;

								if (check_history(i, j, k, l, m, n))
									continue;


								printf("%d | %02d | %02d | %02d | %02d | %02d | %02d---lost=%d, sum=%d, remain3(%d, %d, %d, %d, %d, %d)\n",
									total, i, j, k, l, m, n, avail_lost, i+j+k+l+m+n, i%3, j%3, k%3, l%3, m%3, n%3);
								total++;

							}



#if 0
	for (i = 21; i < 184; i++)
	{
		percent = (float)(count[i]*100.0)/total_num;
		printf("%d has : count=%d, percnet=%2.2f%%\n", i, count[i], percent);

		if (count[i] > count1)
		{
			count3 = count2;
			max3 = max2;
			count2 = count1;
			max2 = max1;

			count1 = count[i];
			max1 = i;
		}
		else if (count[i] > count2)
		{
			count3 = count2;
			max3 = max2;

			count2 = count[i];
			max2 = i;			
		}
		else if (count[i] > count3)
		{
			count3 = count[i];
			max3 = i;			
		}
		
	}

	printf("1: sum=%d, count=%d,  percnet=%2.2f\%\n", max1, count1, (float)(count1*100.0)/total_num);
	printf("2: sum=%d, count=%d,  percnet=%2.2f\%\n", max2, count2, (float)(count2*100.0)/total_num);
	printf("3: sum=%d, count=%d,  percnet=%2.2f\%\n", max3, count3, (float)(count3*100.0)/total_num);
#endif

	printf("all total=%d\n", total);
}

int blue_count()
{
	int i;
	int count[17];
	int count_odd = 0, count_even = 0;
	int z1=0, z2=0, z3=0, z4=0;
	int remain4;
	int y1=0, y2=0, y3=0, y4=0;
	
	memset(count, 0, sizeof(count));

	for (i=0; i<total_num; i++)
	{
		count[total_ssq[i].data.b] += 1;

		if (total_ssq[i].data.b%2)
			count_odd++;
		else
			count_even++;

		remain4=total_ssq[i].data.b%4;
		switch (remain4)
		{
			case 0:
				z1++;
				break;
			case 1:
				z2++;
				break;
			case 2:
				z3++;
				break;
			case 3:
				z4++;
				break;
			default:
				printf("blue remain 4 error!\n");
				exit(1);
		}

		if (total_ssq[i].data.b<=4)
			y1++;
		else if (total_ssq[i].data.b<=8)
			y2++;
		else if (total_ssq[i].data.b<=12)
			y3++;
		else
			y4++;
	}

	for (i=1; i<17; i++)
	{
		printf("blue=%d, count=%d, percent=%2.2f, lost=%d\n", i, count[i], (float)(count[i]*100.0)/total_num, lost_b[i]);
	}

	printf("blue small=%d, big=%d, odd=%d, even=%d\n", (y1+y2), (y3+y4), count_odd, count_even);
	printf("blue zone: (1~4:%d, 5~8:%d, 9~12:%d, 13~16:%d)\n", y1, y2, y3, y4);
	printf("blue 4 remain: (0:%d, 1:%d, 2:%d, 3:%d)\n", z1, z2, z3, z4);

	return 0;
}

int red_count()
{
	int i;
	int count[34];
	memset(count, 0, sizeof(count));

	for (i=0; i<total_num; i++)
	{
		count[total_ssq[i].data.r1] += 1;
		count[total_ssq[i].data.r2] += 1;
		count[total_ssq[i].data.r3] += 1;
		count[total_ssq[i].data.r4] += 1;
		count[total_ssq[i].data.r5] += 1;
		count[total_ssq[i].data.r6] += 1;
		
	}

	for (i=1; i<34; i++)
	{
		printf("red=%d, count=%d, percent=%2.2f, lost=%d\n", i, count[i], (float)(count[i]*100.0)/total_num, lost_r[i]);
	}

	return 0;
}

void data_stat()
{
	int i;
	int zone_kind[28];
	int odd_even_kind[7];
	int r_remain_kind[6][3];
	int follow_red[6][34];
	int follow_blue[17];

	memset(zone_kind, 0, sizeof(zone_kind));
	memset(odd_even_kind, 0, sizeof(odd_even_kind));
	memset(r_remain_kind, 0, sizeof (r_remain_kind));
	memset(follow_blue, 0, sizeof(follow_blue));
	memset(follow_red, 0, sizeof(follow_red));

	for (i = total_num-1; i>=0; i--)
	{
#if 1
		if ((zone1[i]==6)&&(zone2[i]==0)&&(zone3[i]==0))
		{
			zone_kind[0]++;
		}
		else if ((zone1[i]==5)&&(zone2[i]==1)&&(zone3[i]==0))
		{
			zone_kind[1]++;
		}
		else if ((zone1[i]==5)&&(zone2[i]==0)&&(zone3[i]==1))
		{
			zone_kind[2]++;
		}
		else if ((zone1[i]==4)&&(zone2[i]==2)&&(zone3[i]==0))
		{
			zone_kind[3]++;
		}
		else if ((zone1[i]==4)&&(zone2[i]==1)&&(zone3[i]==1))
		{
			zone_kind[4]++;
		}
		else if ((zone1[i]==4)&&(zone2[i]==0)&&(zone3[i]==2))
		{
			zone_kind[5]++;
		}
		else if ((zone1[i]==3)&&(zone2[i]==3)&&(zone3[i]==0))
		{
			zone_kind[6]++;
		}
		else if ((zone1[i]==3)&&(zone2[i]==2)&&(zone3[i]==1))
		{
			zone_kind[7]++;
		}
		else if ((zone1[i]==3)&&(zone2[i]==1)&&(zone3[i]==2))
		{
			zone_kind[8]++;
		}
		else if ((zone1[i]==3)&&(zone2[i]==0)&&(zone3[i]==3))
		{
			zone_kind[9]++;
		}
		else if ((zone1[i]==2)&&(zone2[i]==4)&&(zone3[i]==0))
		{
			zone_kind[10]++;
		}
		else if ((zone1[i]==2)&&(zone2[i]==3)&&(zone3[i]==1))
		{
			zone_kind[11]++;
		}
		else if ((zone1[i]==2)&&(zone2[i]==2)&&(zone3[i]==2))
		{
			zone_kind[12]++;
		}
		else if ((zone1[i]==2)&&(zone2[i]==1)&&(zone3[i]==3))
		{
			zone_kind[13]++;
		}
		else if ((zone1[i]==2)&&(zone2[i]==0)&&(zone3[i]==4))
		{
			zone_kind[14]++;
		}
		else if ((zone1[i]==1)&&(zone2[i]==5)&&(zone3[i]==0))
		{
			zone_kind[15]++;
		}
		else if ((zone1[i]==1)&&(zone2[i]==4)&&(zone3[i]==1))
		{
			zone_kind[16]++;
		}
		else if ((zone1[i]==1)&&(zone2[i]==3)&&(zone3[i]==2))
		{
			zone_kind[17]++;
		}
		else if ((zone1[i]==1)&&(zone2[i]==2)&&(zone3[i]==3))
		{
			zone_kind[18]++;
		}
		else if ((zone1[i]==1)&&(zone2[i]==1)&&(zone3[i]==4))
		{
			zone_kind[19]++;
		}
		else if ((zone1[i]==1)&&(zone2[i]==0)&&(zone3[i]==5))
		{
			zone_kind[20]++;
		}
		else if ((zone1[i]==0)&&(zone2[i]==6)&&(zone3[i]==0))
		{
			zone_kind[21]++;
		}
		else if ((zone1[i]==0)&&(zone2[i]==5)&&(zone3[i]==1))
		{
			zone_kind[22]++;
		}
		else if ((zone1[i]==0)&&(zone2[i]==4)&&(zone3[i]==2))
		{
			zone_kind[23]++;
		}
		else if ((zone1[i]==0)&&(zone2[i]==3)&&(zone3[i]==3))
		{
			zone_kind[24]++;
		}
		else if ((zone1[i]==0)&&(zone2[i]==2)&&(zone3[i]==4))
		{
			zone_kind[25]++;
		}
		else if ((zone1[i]==0)&&(zone2[i]==1)&&(zone3[i]==5))
		{
			zone_kind[26]++;
		}
		else if ((zone1[i]==0)&&(zone2[i]==0)&&(zone3[i]==6))
		{
			zone_kind[27]++;
		}
#endif
#if 1
		if ((data_odd[i]==6)&&(data_even[i]==0))
		{
			odd_even_kind[0]++;
		}
		else if ((data_odd[i]==5)&&(data_even[i]==1))
		{
			odd_even_kind[1]++;
		}
		else if ((data_odd[i]==4)&&(data_even[i]==2))
		{
			odd_even_kind[2]++;
		}
		else if ((data_odd[i]==3)&&(data_even[i]==3))
		{
			odd_even_kind[3]++;
		}
		else if ((data_odd[i]==2)&&(data_even[i]==4))
		{
			odd_even_kind[4]++;		
		}
		else if ((data_odd[i]==1)&&(data_even[i]==5))
		{
			odd_even_kind[5]++;
		}
		else if ((data_odd[i]==0)&&(data_even[i]==6))
		{
			odd_even_kind[6]++;
		}
#endif
#if 1
		if (r1_remain[i]==0)
		{
			r_remain_kind[0][0]++;
		}
		else if (r1_remain[i] == 1)
		{
			r_remain_kind[0][1]++;
		}
		else if (r1_remain[i] == 2)
		{
			r_remain_kind[0][2]++;
		}

		if (r2_remain[i]==0)
		{
			r_remain_kind[1][0]++;
		}
		else if (r2_remain[i] == 1)
		{
			r_remain_kind[1][1]++;
		}
		else if (r2_remain[i] == 2)
		{
			r_remain_kind[1][2]++;
		}

		if (r3_remain[i]==0)
		{
			r_remain_kind[2][0]++;
		}
		else if (r3_remain[i] == 1)
		{
			r_remain_kind[2][1]++;
		}
		else if (r3_remain[i] == 2)
		{
			r_remain_kind[2][2]++;
		}

		if (r4_remain[i]==0)
		{
			r_remain_kind[3][0]++;
		}
		else if (r4_remain[i] == 1)
		{
			r_remain_kind[3][1]++;
		}
		else if (r4_remain[i] == 2)
		{
			r_remain_kind[3][2]++;
		}

		if (r5_remain[i]==0)
		{
			r_remain_kind[4][0]++;
		}
		else if (r5_remain[i] == 1)
		{
			r_remain_kind[4][1]++;
		}
		else if (r5_remain[i] == 2)
		{
			r_remain_kind[4][2]++;
		}

		if (r6_remain[i]==0)
		{
			r_remain_kind[5][0]++;
		}
		else if (r6_remain[i] == 1)
		{
			r_remain_kind[5][1]++;
		}
		else if (r6_remain[i] == 2)
		{
			r_remain_kind[5][2]++;
		}
#endif
#if 1
		if (total_ssq[i].data.r1 == total_ssq[0].data.r1)
		{
			if (i > 0)
			{
				follow_red[0][total_ssq[i-1].data.r1]++;
			}
		}
		if (total_ssq[i].data.r2 == total_ssq[0].data.r2)
		{
			if (i > 0)
			{
				follow_red[1][total_ssq[i-1].data.r2]++;
			}
		}
		if (total_ssq[i].data.r3 == total_ssq[0].data.r3)
		{
			if (i > 0)
			{
				follow_red[2][total_ssq[i-1].data.r3]++;
			}
		}
		if (total_ssq[i].data.r4 == total_ssq[0].data.r4)
		{
			if (i > 0)
			{
				follow_red[3][total_ssq[i-1].data.r4]++;
			}
		}
		if (total_ssq[i].data.r5 == total_ssq[0].data.r5)
		{
			if (i > 0)
			{
				follow_red[4][total_ssq[i-1].data.r5]++;
			}
		}
		if (total_ssq[i].data.r6 == total_ssq[0].data.r6)
		{
			if (i > 0)
			{
				follow_red[5][total_ssq[i-1].data.r6]++;
			}
		}
		if (total_ssq[i].data.b == total_ssq[0].data.b)
		{
			if (i > 0)
			{
				//printf("%d | %02d | %02d | %02d | %02d | %02d | %02d || %02d\n", total_ssq[i].no, total_ssq[i].data.r1, total_ssq[i].data.r2, total_ssq[i].data.r3, total_ssq[i].data.r4, total_ssq[i].data.r5, total_ssq[i].data.r6, total_ssq[i].data.b);
				//printf("%d | %02d | %02d | %02d | %02d | %02d | %02d || %02d\n", total_ssq[i-1].no, total_ssq[i-1].data.r1, total_ssq[i-1].data.r2, total_ssq[i-1].data.r3, total_ssq[i-1].data.r4, total_ssq[i-1].data.r5, total_ssq[i-1].data.r6, total_ssq[i-1].data.b);
				follow_blue[total_ssq[i-1].data.b]++;
			}
		}
#endif
	}

	printf("zone statistic:\n");
	printf("(2, 2, 2)=%d\n", zone_kind[12]);
	printf("(3, 2, 1)=%d, (3, 1, 2)=%d, (2, 3, 1)=%d, (2, 1, 3)=%d, (1, 3, 2)=%d, (1, 2, 3)=%d\n",
		zone_kind[7], zone_kind[8], zone_kind[11], zone_kind[13], zone_kind[17], zone_kind[18]);

	printf("(3, 3, 0)=%d, (3, 0, 3)=%d, (0, 3, 3)=%d\n",
		zone_kind[6], zone_kind[9], zone_kind[24]);

	printf("(4, 2, 0)=%d, (4, 0, 2)=%d, (2, 4, 0)=%d, (2, 0, 4)=%d, (0, 4, 2)=%d, (0, 2, 4)=%d\n",
		zone_kind[3], zone_kind[5], zone_kind[10], zone_kind[14], zone_kind[23], zone_kind[25]);

	printf("(4, 1, 1)=%d, (1, 4, 1)=%d, (1, 1, 4)=%d\n",
		zone_kind[4], zone_kind[16], zone_kind[19]);

	printf("(5, 1, 0)=%d, (5, 0, 1)=%d, (1, 5, 0)=%d, (1, 0, 5)=%d, (0, 5, 1)=%d, (0, 1, 5)=%d\n",
		zone_kind[1], zone_kind[2], zone_kind[15], zone_kind[20], zone_kind[22], zone_kind[26]);

	printf("(6, 0, 0)=%d, (0, 6, 0)=%d, (0, 0, 6)=%d\n",
		zone_kind[0], zone_kind[21], zone_kind[27]);

	printf("old even statistic:\n");
	printf("(3, 3)=%d\n", odd_even_kind[3]);
	printf("(4, 2)=%d, (2, 4)=%d\n", odd_even_kind[2], odd_even_kind[4]);
	printf("(5, 1)=%d, (1, 5)=%d\n", odd_even_kind[1], odd_even_kind[5]);
	printf("(6, 0)=%d, (0, 6)=%d\n", odd_even_kind[0], odd_even_kind[6]);

	printf("red remainder:\n");
	printf("1---(0:%d)(1:%d)(2:%d)\n", r_remain_kind[0][0], r_remain_kind[0][1], r_remain_kind[0][2]);
	printf("2---(0:%d)(1:%d)(2:%d)\n", r_remain_kind[1][0], r_remain_kind[1][1], r_remain_kind[1][2]);
	printf("3---(0:%d)(1:%d)(2:%d)\n", r_remain_kind[2][0], r_remain_kind[2][1], r_remain_kind[2][2]);
	printf("4---(0:%d)(1:%d)(2:%d)\n", r_remain_kind[3][0], r_remain_kind[3][1], r_remain_kind[3][2]);
	printf("5---(0:%d)(1:%d)(2:%d)\n", r_remain_kind[4][0], r_remain_kind[4][1], r_remain_kind[4][2]);
	printf("6---(0:%d)(1:%d)(2:%d)\n", r_remain_kind[5][0], r_remain_kind[5][1], r_remain_kind[5][2]);

	printf("history follow first red=%d:\n", total_ssq[0].data.r1);
	for (i=1; i<=33; i++)
	{
		printf("%d->%d ", i, follow_red[0][i]);
	}
	printf("\n");

	printf("history follow second red=%d:\n", total_ssq[0].data.r2);
	for (i=1; i<=33; i++)
	{
		printf("%d->%d ", i, follow_red[1][i]);
	}
	printf("\n");

	printf("history follow third red=%d:\n", total_ssq[0].data.r3);
	for (i=1; i<=33; i++)
	{
		printf("%d->%d ", i, follow_red[2][i]);
	}
	printf("\n");

	printf("history follow fourth red=%d:\n", total_ssq[0].data.r4);
	for (i=1; i<=33; i++)
	{
		printf("%d->%d ", i, follow_red[3][i]);
	}
	printf("\n");

	printf("history follow fiveth red=%d:\n", total_ssq[0].data.r5);
	for (i=1; i<=33; i++)
	{
		printf("%d->%d ", i, follow_red[4][i]);
	}
	printf("\n");

	printf("history follow sixth red=%d:\n", total_ssq[0].data.r6);
	for (i=1; i<=33; i++)
	{
		printf("%d->%d ", i, follow_red[5][i]);
	}
	printf("\n");

	printf("history follow blue=%d:\n", total_ssq[0].data.b);
	for (i=1; i<=16; i++)
	{
		printf("%d->%d ", i, follow_blue[i]);
	}
	printf("\n");
}

int check_last_same(int num, int max, int i, int j, int k, int l, int m, int n)
{
	int x;
	int count;
	for (x=0; x<num; x++)
	{
		//printf("check num=%d %02d %02d %02d %02d %02d %02d\n", total_ssq[x].no, total_ssq[x].data.r1, total_ssq[x].data.r2, total_ssq[x].data.r3, total_ssq[x].data.r4, total_ssq[x].data.r5, total_ssq[x].data.r6);
		count = 0;
#if 0
		if ((i==total_ssq[x].data.r1)||(i==total_ssq[x].data.r2)||(i==total_ssq[x].data.r3)
		||(i==total_ssq[x].data.r4)||(i==total_ssq[x].data.r5)||(i==total_ssq[x].data.r6))
		{
			count++;
		}

		if ((j==total_ssq[x].data.r1)||(j==total_ssq[x].data.r2)||(j==total_ssq[x].data.r3)
		||(j==total_ssq[x].data.r4)||(j==total_ssq[x].data.r5)||(j==total_ssq[x].data.r6))
		{
			count++;
		}

		if ((l==total_ssq[x].data.r1)||(l==total_ssq[x].data.r2)||(l==total_ssq[x].data.r3)
		||(l==total_ssq[x].data.r4)||(l==total_ssq[x].data.r5)||(l==total_ssq[x].data.r6))
		{
			count++;
		}

		if ((m==total_ssq[x].data.r1)||(m==total_ssq[x].data.r2)||(m==total_ssq[x].data.r3)
		||(m==total_ssq[x].data.r4)||(m==total_ssq[x].data.r5)||(m==total_ssq[x].data.r6))
		{
			count++;
		}

		if ((k==total_ssq[x].data.r1)||(k==total_ssq[x].data.r2)||(k==total_ssq[x].data.r3)
		||(k==total_ssq[x].data.r4)||(k==total_ssq[x].data.r5)||(k==total_ssq[x].data.r6))
		{
			count++;
		}

		if ((n==total_ssq[x].data.r1)||(n==total_ssq[x].data.r2)||(n==total_ssq[x].data.r3)
		||(n==total_ssq[x].data.r4)||(n==total_ssq[x].data.r5)||(n==total_ssq[x].data.r6))
		{
			count++;
		}
#endif
#if 1
		if ((i==total_ssq[x].data.r1) && (j==total_ssq[x].data.r2))
		{
			count++;
		}
		if ((j==total_ssq[x].data.r2) && (k==total_ssq[x].data.r3))
		{
			count++;
		}
		if ((k==total_ssq[x].data.r3) && (l==total_ssq[x].data.r4))
		{
			count++;
		}
		if ((l==total_ssq[x].data.r4) && (m==total_ssq[x].data.r5))
		{
			count++;
		}
		if ((m==total_ssq[x].data.r5) && (n==total_ssq[x].data.r6))
		{
			count++;
		}
#endif
		//printf("count=%d\n", count);

		if (count >= max)
			return 1;
	}
	return 0;
}

int check_last(int max, int i, int j, int k, int l, int m, int n)
{
	int current=0;
	if ((i==total_ssq[0].data.r1)||(i==total_ssq[0].data.r2)||(i==total_ssq[0].data.r3)
		||(i==total_ssq[0].data.r4)||(i==total_ssq[0].data.r5)||(i==total_ssq[0].data.r6))
	{
		current++;
	}
	if ((j==total_ssq[0].data.r1)||(j==total_ssq[0].data.r2)||(j==total_ssq[0].data.r3)
		||(j==total_ssq[0].data.r4)||(j==total_ssq[0].data.r5)||(j==total_ssq[0].data.r6))
	{
		current++;
	}
	if ((k==total_ssq[0].data.r1)||(k==total_ssq[0].data.r2)||(k==total_ssq[0].data.r3)
		||(k==total_ssq[0].data.r4)||(k==total_ssq[0].data.r5)||(k==total_ssq[0].data.r6))
	{
		current++;
	}
	if ((l==total_ssq[0].data.r1)||(l==total_ssq[0].data.r2)||(l==total_ssq[0].data.r3)
		||(l==total_ssq[0].data.r4)||(l==total_ssq[0].data.r5)||(l==total_ssq[0].data.r6))
	{
		current++;
	}
	if ((m==total_ssq[0].data.r1)||(m==total_ssq[0].data.r2)||(m==total_ssq[0].data.r3)
		||(m==total_ssq[0].data.r4)||(m==total_ssq[0].data.r5)||(m==total_ssq[0].data.r6))
	{
		current++;
	}
	if ((n==total_ssq[0].data.r1)||(n==total_ssq[0].data.r2)||(n==total_ssq[0].data.r3)
		||(n==total_ssq[0].data.r4)||(n==total_ssq[0].data.r5)||(n==total_ssq[0].data.r6))
	{
		current++;
	}
	if (current > max)
		return 1;
	else
		return 0;
}

int check_history(int i, int j, int k, int l, int m, int n)
{
	int num;
	for (num = total_num-1; num>=0; num--)
	{
		if ((i==total_ssq[num].data.r1)&&(j==total_ssq[num].data.r2)&&(k==total_ssq[num].data.r3)
			&&(l==total_ssq[num].data.r4)&&(m==total_ssq[num].data.r5)&&(n==total_ssq[num].data.r6))
			return 1;
	}

	return 0;
}

int main(int argc, char **argv)
{
	int ret;

	ret = scan_data(DATA_FILE_PATH);
	if (ret != 0)
	{
		printf("scan data fail!\n");
	}

	count_sum();
	data_stat();
	conbination(1, 33, 6);
	blue_count();
	red_count();

	return 0;
}


